package com.camel.docker.controller;

import com.camel.docker.config.FTPClientFactory;
import com.camel.docker.config.FTPConfig;
import com.camel.docker.entity.Result;
import com.camel.docker.utils.FTPUtils;
import com.camel.docker.utils.ResultUtil;
import org.apache.commons.net.ftp.FTPFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

@RestController
@RequestMapping("/file")
public class FilesController extends BaseController {

    @Autowired
    private FTPUtils ftpUtils;

    @Autowired
    private FTPClientFactory ftpClientFactory;

    @Autowired
    private FTPConfig ftpConfig;

    @PostMapping
    public Result upload(MultipartFile file, String path) {
        boolean flag = false;
        try {
            flag = ftpUtils.uploadFile(path, file.getOriginalFilename(), file.getInputStream());
        } catch (IOException e) {
            flag = false;
            throw new RuntimeException("文件上传错误");
        }
        return flag ? ResultUtil.success("上传成功") : ResultUtil.error(500, "上传失败");
    }

    @GetMapping
    public Result index(String dir) {
        FTPFile[] ftpFiles = new FTPFile[]{};
        try {

            ftpFiles = ftpUtils.getFiles(dir);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtil.success(ftpFiles);
    }

    @DeleteMapping
    public Result delete(String name, String path) {
        boolean result = false;
        try {
            result = ftpUtils.deleteFile(path, name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result ? ResultUtil.success("删除文件成功") : ResultUtil.error(500, "删除文件失败");
    }

    @PostMapping("/directory")
    public Result directory(String path, String name) {
        boolean result = false;
        try {
            result = ftpUtils.makeDirectory(path, name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result ? ResultUtil.success("创建文件夹成功") : ResultUtil.error(500, "创建文件夹失败");
    }

    @GetMapping("/download")
    public void download(String path, String name, HttpServletResponse response) {
        // 设置response头信息
        response.reset();
        // 改成输出excel文件
        response.setContentType("application/octet-stream");
        response.setHeader("content-type", "application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;fileName=" + name);
        InputStream in = null;
        OutputStream os = null;
        BufferedInputStream bis = null;
        try {
            in = ftpUtils.download(path, name);
            os = response.getOutputStream();
            bis = new BufferedInputStream(in);
            byte[] buffer = new byte[1024];
            int i = bis.read(buffer);
            while (i != -1) {
                os.write(buffer, 0, i);
                i = bis.read(buffer);
            }
        } catch (Exception e) {
            e.printStackTrace();

        }finally {
            try {
                bis.close();
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
